//
//  ThemeImageView.m
//  Custom_Weibo
//
//  Created by LEO on 12/9/15.
//  Copyright © 2015 Leo_Lei. All rights reserved.
//

#import "ThemeImageView.h"

@implementation ThemeImageView

#pragma mark - INITIALIZATION_METHODS
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //  When picture is added, use its original size.
        _topCapHeight = 0.0;
        _leftCapWidth = 0.0;
        [self getNotification];
    }
    return self;
}

- (void)awakeFromNib {
    _topCapHeight = 0.0;
    _leftCapWidth = 0.0;
    [self getNotification];
}

#pragma mark - MONITOR_NOTIFICATION
- (void)getNotification {
    //  Monitoring the notification when theme changed.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(themeDidChange:)
                                                 name:kThemeNotification
                                               object:nil];
}

#pragma mark - WHEN_THEME_CHANGED
- (void)themeDidChange:(NSNotification *)notification {
    [self loadImage];
}

#pragma mark SET_IMAGE
- (void)setImageName:(NSString *)imageName {
    if (_imageName != imageName) {
        _imageName = [imageName copy];
        [self loadImage];
    }
}

#pragma mark LOAD_IMAGE
- (void)loadImage {
    //  Get the object and add corresponding image on the image view.
    UIImage *image = [[ThemeManager defaultManager] getThemeImage:_imageName];
    self.image = [image stretchableImageWithLeftCapWidth:_leftCapWidth
                                            topCapHeight:_topCapHeight];
}

#pragma mark - DEALLOC
- (void)dealloc {
    //  Remove the observer.
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
